package com.ciba.demo.util;

import java.sql.*;

public class JDBCUtils {
    /**
     * 构造函数
     */
    private static String user ;
    private static String passwords ;
    private static String url;
    private static String classname ;

    private JDBCUtils(){
    }

    static
    {
        classname = "com.mysql.cj.jdbc.Driver" ;
        url = "jdbc:mysql://localhost:3306/ciba" ;
        user = "root";
        passwords = "root" ;

        try {
            Class.forName(classname) ;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return 返回与数据库的conn
     */
    public static Connection getconnection() throws SQLException {
        return DriverManager.getConnection(url,user,passwords) ;
    }

    public static void close(Connection conn, Statement state, ResultSet re){
        if(re!=null){
            try {
                re.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if(state!=null){
            try {
                state.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}